import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# 示例数据
X = np.array([[1], [2], [3], [4], [5]])  # 自变量
y = np.array([2, 3.5, 6, 7, 9])  # 因变量

# 创建线性回归模型
model = LinearRegression()
model.fit(X, y)

# 获取模型的参数
beta_0 = model.intercept_  # 截距
beta_1 = model.coef_[0]    # 斜率

# 绘制数据点和回归线
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', label='Regression line')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

print(f"回归方程为: y = {beta_0:.2f} + {beta_1:.2f} * x")